const {
    createCategory,
    updateCategory,
    removeCategory,
    restoreCategory,
    findCategory,
    findCategoryInfo,
    changeCategoryStatus
} = require('../service/category.service')
const {
    updateInformationRecord
} = require("../service/information.service");

const {sucOperation} = require("../constant/success.type");
const {failOperation} = require("../constant/err.type");
const {findIsCollect} = require("../service/collect.service");
const moment = require("moment");

class CategoryController {
    async updateCategoryRecord(ctx) {
        try {
            const {record_info} = await findCategoryInfo(ctx.params.id)
            await updateInformationRecord(
                record_info.id, ctx.request.body.type
            );
            ctx.body = {
                ...sucOperation,
            };
        } catch (err) {
            return (ctx.body = {
                ...failOperation,
            });
        }
    }
    async create(ctx, next) {
        try {
            const {end_time} = ctx.request.body
            if (moment(end_time).isBefore(moment())) {
                return ctx.body = {
                    ...failOperation,
                    message: '结束时间不能小于当前时间'
                }
            }
            const { createdAt, updatedAt, ...res } = await createCategory(
                {
                    ...ctx.request.body,
                    user_id: ctx.state.user.id,
                }
            );
            ctx.body = {
                ...sucOperation,
                result: res,
            };
        } catch (err) {
            return ctx.body = {
                ...failOperation
            };
        }
    }

    async update(ctx) {
        try {
            const res = await updateCategory(ctx.params.id, ctx.request.body);
            if (res) {
                ctx.body = {
                    ...sucOperation,
                    result: res,
                };
            } else {
                return ctx.body = {
                    ...failOperation
                };
            }
        } catch (err) {
            console.error(err);
        }
    }

    async remove(ctx) {
        const res = await removeCategory(ctx.params.id);

        if (res) {
            ctx.body = {
                ...sucOperation,
            };
        } else {
            return ctx.body = {
                ...failOperation
            };
        }
    }

    async restore(ctx) {
        const res = await restoreGoods(ctx.params.id);
        if (res) {
            ctx.body = {
                ...sucOperation,
            };
        } else {
            return ctx.body = {
                ...failOperation
            };
        }
    }

    async findAll(ctx) {
        // 1. 解析pageNum和pageSize
        const {
            pageNum = 1,
            pageSize = 10,
            ...params
        } = ctx.request.body;
        await changeCategoryStatus()
        // 2. 调用数据处理的相关方法
        const res = await findCategory({ pageNum, pageSize,...params });
        // 3. 返回结果
        ctx.body = {
            ...sucOperation,
            result: res,
        };
    }

    async findAllByUser(ctx) {
        // 1. 解析pageNum和pageSize
        const {
            pageNum = 1,
            pageSize = 10,
        } = ctx.request.body;
        await changeCategoryStatus()
        // 2. 调用数据处理的相关方法
        const res = await findCategory({ pageNum, pageSize,user_id:ctx.state.user.id });
        // 3. 返回结果
        ctx.body = {
            ...sucOperation,
            result: res,
        };
    }

    async findOne(ctx) {
        const { id } = ctx.params;
        const result = await findCategoryInfo(id);
        const {id:user_id} = ctx.state.user
        const isCollect = await findIsCollect({cat_id:id,user_id})
        if (result) {
            ctx.body = {
                ...sucOperation,
                result:{
                    ...result,
                    isCollect
                },

            };
        } else {
            ctx.body = {
                code: 500,
                message: "id有误",
            };
        }
    }
}

module.exports = new CategoryController()
